package com.malty.mall.controller.ums;

import com.malty.mall.common.CommonResult;
import com.malty.mall.domain.dto.req.UmsRoleParam;
import com.malty.mall.service.ums.IUmsRoleResourceService;
import com.malty.mall.service.ums.IUmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author malty
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("/role")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UmsRoleController {
    private final IUmsRoleService umsRoleService;
    private final IUmsRoleResourceService roleResourceService;

    @ApiOperation("创建角色")
    @PostMapping("/create")
    public CommonResult<Boolean> createUmsRole(@Validated @RequestBody UmsRoleParam umsRoleParam) {
        return CommonResult.success(umsRoleService.create(umsRoleParam));
    }

    @ApiOperation("关联资源")
    @PostMapping("/relate")
    public CommonResult<Boolean> relateUmsResource(@RequestParam Integer roleId, @RequestParam Integer resourceId) {
        return CommonResult.success(roleResourceService.createOrUpdateRelation(roleId, resourceId));
    }

    @GetMapping("/list")
    @ApiOperation("查询所有角色")
    public CommonResult<List<UmsRoleParam>> listUmsRole() {
        return CommonResult.success(umsRoleService.listAll());
    }

    @ApiOperation("查询所有角色")
    @GetMapping("/list_by_uid")
    public CommonResult<List<UmsRoleParam>> listUmsRole(Integer uid) {
        return CommonResult.success(umsRoleService.listByUid(uid));
    }
}
